package com.autumn.spring.boot.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

import com.autumn.util.StringUtils;

/**
 * 授权
 * 
 * @author 老码农
 *         <p>
 *         Description
 *         </p>
 * @date 2017-11-04 02:25:49
 */
@ConfigurationProperties(prefix = AutumnAuthProperties.PREFIX)
public class AutumnAuthProperties extends AbstractAutumnProperties {

	/**
	 * 
	 */
	private static final long serialVersionUID = 808855799819234501L;

	/**
	 * 属性前缀
	 */
	public final static String PREFIX = "autumn.auth";

	/**
	 * 默认 Token 开头
	 */
	public static final String DEFAULT_TOKEN_HEAD = "Basic";

	/**
	 * 默认 缓存键前缀
	 */
	public static final String DEFAULT_CACHE_KEY_PREFIX = "autumn_redis_auth_cache:";

	/**
	 * 默认 会话键前缀
	 */
	public static final String DEFAULT_SESSION_KEY_PREFIX = "autumn_redis_session:";

	/**
	 * 内存缓存提供程序
	 */
	public static final String CACHE_PROVIDER_MEMORY = "memory";

	/**
	 * Redis缓存提供程序
	 */
	public static final String REDIS_PROVIDER_MEMORY = "redis";

	private String tokenSecret;
	private Integer expire = 3600;
	private String tokenHead = DEFAULT_TOKEN_HEAD;
	private String tokenbase64 = null;
	private String cacheKeyPrefix = DEFAULT_CACHE_KEY_PREFIX;
	private String sessionKeyPrefix = DEFAULT_SESSION_KEY_PREFIX;
	private String cacheProvider = CACHE_PROVIDER_MEMORY;
	private AuthRedisProperties redis;
	private boolean isRememberMe;

	/**
	 * 
	 */
	public AutumnAuthProperties() {
		this.setRedis(new AuthRedisProperties());
	}

	/**
	 * 是否是 Redis 提供缓存
	 * @return
	 */
	public boolean isRedisCacheProvider() {
		return REDIS_PROVIDER_MEMORY.equalsIgnoreCase(this.getCacheProvider());
	}

	/**
	 * 获取基于 token 的密钥
	 * 
	 * @return
	 */
	public String getTokenSecret() {
		return tokenSecret;
	}

	/**
	 * token 编码为Base64
	 * 
	 * @return 2017-12-12 17:10:36
	 */
	public String tokenDecodeBase64() {
		if (tokenbase64 != null) {
			return tokenbase64;
		}
		if (this.getTokenSecret() == null) {
			return null;
		}
		synchronized (this) {
			tokenbase64 = StringUtils.encodeBase64Utf8String(this.getTokenSecret());
			return tokenbase64;
		}
	}

	/**
	 * 设置基于 token 的密钥
	 * 
	 * @param tokenSecret token密钥
	 */
	public void setTokenSecret(String tokenSecret) {
		this.tokenSecret = tokenSecret;
	}

	/**
	 * 获取过期(秒)
	 * 
	 * @return
	 */
	public Integer getExpire() {
		return expire;
	}

	/**
	 * 设置过期
	 * 
	 * @param expire 秒钟,小于或等于0表示永不过期
	 */
	public void setExpire(Integer expire) {
		this.expire = expire;
	}

	/**
	 * 获取 Token 开头
	 * 
	 * @return
	 */
	public String getTokenHead() {
		if (StringUtils.isNullOrBlank(this.tokenHead)) {
			return DEFAULT_TOKEN_HEAD;
		}
		return tokenHead;
	}

	/**
	 * 设置 Token 开头
	 * 
	 * @param tokenHead
	 */
	public void setTokenHead(String tokenHead) {
		this.tokenHead = tokenHead;
	}

	/**
	 * 获取是否记住我
	 * 
	 * @return
	 */
	public boolean isRememberMe() {
		return isRememberMe;
	}

	/**
	 * 设置是否记住我
	 * 
	 * @param isRememberMe 记住我
	 */
	public void setRememberMe(boolean isRememberMe) {
		this.isRememberMe = isRememberMe;
	}

	/**
	 * 获取缓存键前缀
	 * 
	 * @return
	 */
	public String getCacheKeyPrefix() {
		if (StringUtils.isNullOrBlank(this.cacheKeyPrefix)) {
			return DEFAULT_CACHE_KEY_PREFIX;
		}
		return cacheKeyPrefix;
	}

	/**
	 * 设置缓存键前缀
	 * 
	 * @param cacheKeyPrefix 缓存键前缀
	 */
	public void setCacheKeyPrefix(String cacheKeyPrefix) {
		this.cacheKeyPrefix = cacheKeyPrefix;
	}

	/**
	 * 获取会话键前缀
	 * 
	 * @return
	 */
	public String getSessionKeyPrefix() {
		if (StringUtils.isNullOrBlank(this.sessionKeyPrefix)) {
			return DEFAULT_SESSION_KEY_PREFIX;
		}
		return sessionKeyPrefix;
	}

	/**
	 * 设置会话键前缀
	 * 
	 * @param sessionKeyPrefix 会话键前缀
	 */
	public void setSessionKeyPrefix(String sessionKeyPrefix) {
		this.sessionKeyPrefix = sessionKeyPrefix;
	}

	/**
	 * 获取缓存提供程序
	 * 
	 * @return
	 */
	public String getCacheProvider() {
		if (cacheProvider != null) {
			return cacheProvider.trim();
		}
		return cacheProvider;
	}

	/**
	 * 设置缓存提供程序
	 * 
	 * @param cacheProvider 提供程序
	 */
	public void setCacheProvider(String cacheProvider) {
		this.cacheProvider = cacheProvider;
	}

	/**
	 * 获取 Redis 配置
	 * 
	 * @return
	 */
	public AuthRedisProperties getRedis() {
		return redis;
	}

	/**
	 * 设置
	 * 
	 * @param redis
	 */
	public void setRedis(AuthRedisProperties redis) {
		this.redis = redis;
	}

	/**
	 * 授权 Auth Redis 属性
	 * 
	 * @author 杨昌国 2018-12-06 12:39:28
	 */
	public class AuthRedisProperties extends AbstractRedisProperties {

		/**
		 * 
		 */
		private static final long serialVersionUID = 6113162939282188516L;
	}
}
